/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cn.ekuma.epos.datalogic.define.dao;

import cn.ekuma.data.dao.BaseDAO;
import cn.ekuma.util.JsonUtil;

import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.DataRead;
import com.openbravo.data.loader.DataWrite;
import com.openbravo.data.loader.Datas;
import com.openbravo.data.loader.I_Session;
import com.openbravo.data.loader.PreparedSentence;
import com.openbravo.data.loader.SerializerReadString;
import com.openbravo.data.loader.SerializerWriteString;
import com.openbravo.data.loader.Session;
import com.openbravo.data.loader.TableDefinition;
import com.openbravo.data.model.Field;
import com.openbravo.format.Formats;
import com.openbravo.bean.Receipt;
import com.openbravo.data.loader.ImageUtils;
import java.util.HashMap;

/**
 *
 * @author Administrator
 */
public class ReceiptDAO extends BaseDAO<Receipt>{

    public ReceiptDAO(I_Session s) {
        super(s);
    }

    @Override
    public TableDefinition getTable() {
        return new TableDefinition(s,
                    "RECEIPTS",
                    new Field[]{
                          new Field("ID",Datas.STRING,Formats.STRING),
                          new Field("MONEY",Datas.STRING,Formats.STRING),
                          new Field("DATENEW",Datas.TIMESTAMP,Formats.TIMESTAMP),
                          new Field("ATTRIBUTES",Datas.STRING,Formats.STRING),
                          new Field("LASTMODIFIED",Datas.TIMESTAMP, Formats.TIMESTAMP)
                   } ,
                   new int[]{0}
                );
    }

    @Override
    public void writeInsertValues(DataWrite dp, Receipt obj) throws BasicException {
                        dp.setString(1, obj.id);
                        dp.setString(2, obj.momey);
                        dp.setTimestamp(3, obj.dateNew);
                        dp.setString(4, JsonUtil.toString(obj.getAttributes()));
                        dp.setTimestamp(5, obj.getLastModified());
    }

    @Override
    public Class getSuportClass() {
        return Receipt.class;
    }

    public Receipt readValues(DataRead dr,Receipt obj) throws BasicException {
          if(obj==null)
        	  obj=new Receipt();
          obj.setId(dr.getString(1));
          obj.setMomey(dr.getString(2));
          obj.setDateNew(dr.getTimestamp(3));
          obj.setAttributes(JsonUtil.getHashMap(dr.getString(4)));
          obj.setLastModified(dr.getTimestamp(5));
          return obj;
    }
    
    public final String getTicketCash(String ticketId)throws BasicException {
        return (String)new PreparedSentence(s,
                "SELECT MONEY FROM RECEIPTS WHERE  ID = ?",
                SerializerWriteString.INSTANCE,
                SerializerReadString.INSTANCE).find(ticketId);
    }

}
